package ${(projectConfig.projectPackageName)!}.common.security.handler;

import cn.aezo.utils.base.MiscU;
import cn.aezo.utils.base.ValidU;
import ${(projectConfig.projectPackageName)!}.base.BaseKeys;
import ${(projectConfig.projectPackageName)!}.common.security.jwt.JwtUserDetails;
import ${(projectConfig.projectPackageName)!}.common.security.jwt.SecurityJwtTokenUtils;
import ${(projectConfig.projectPackageName)!}.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
public class LoginFinishHandler {
    private Logger logger = LoggerFactory.getLogger(LoginFinishHandler.class);

    @Autowired
    private SecurityJwtTokenUtils jwtUtils;

    @Component
    public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
            HttpSession session = httpServletRequest.getSession();
            JwtUserDetails userDetails = (JwtUserDetails) authentication.getPrincipal();
            session.setAttribute(BaseKeys.SessionUserInfo, userDetails);

            logger.info("{} 登录成功", userDetails.getUsername());

            // httpServletResponse.sendRedirect("/manage/");
            String accessToken = jwtUtils.generateAccessToken(userDetails);
            String refreshToken = jwtUtils.generateRefreshToken(userDetails);

            if(ValidU.isEmpty(accessToken) || ValidU.isEmpty(refreshToken)) {
                BaseController.writeError(httpServletResponse, "获取令牌失败");
                return;
            }

            // TODO 登录成功数据返回
            BaseController.writeSuccess(httpServletResponse, "登录成功", MiscU.Instance.toMap(
                BaseKeys.AccessToken, accessToken,
                BaseKeys.RefreshToken, refreshToken
            ));
        }
    }

    @Component
    public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
        @Override
        public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
            logger.info("登录失败");
            BaseController.writeError(httpServletResponse, e.getMessage());
        }
    }
}
